package controllers;
import business.ApiData;
import business.ApiService;
import models.*;
import net.sf.json.JSONObject;
import play.Logger;
import play.cache.Cache;
import play.mvc.Before;
import play.mvc.Controller;
import utils.ErrorInfo;
import utils.JPAUtil;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

/***
 * code = 1 操作成功  >1 跳转提示
 * code = 0 登陆失效, <0 toast提示
 */
public class ApiController extends Controller {
    @Before
    private static void crosDomain(){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods" , "GET,POST,PUT,DELETE,OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        Logger.info(request.action+","+JSONObject.fromObject(params.all()).toString());
    }
    @Before(unless = {
            "ApiController.login",
            "ApiController.isUpdata",
            "ApiController.isLogin",
            "ApiController.listCity"
    })
    private static void auth(String uid,String token){
        ApiData apiData = new ApiData();
        if (!ApiService.isLoginByApi(uid, token)) {
            apiData.msg = "登录后操作";
            apiData.code = 0;
            renderJSON(apiData);
        }
    }

    public static void isLogin(String uid,String token){
        ApiData apiData = new ApiData();
        if (!ApiService.isLoginByApi(uid, token)) {
            apiData.msg = "登录后操作";
            apiData.code = 0;
        }else{
            apiData.msg = "登录状态正确...";
            apiData.code = 1;
        }
        renderJSON(apiData);
    }
    // 登录
    public static void login(String uname,String pwd){
        ApiData apiData = new ApiData();
        List<sys_user> sys_userList = sys_user.find(" from sys_user a where a.username =?1 and a.password=?2 and status=1 ",uname,pwd).fetch();
        Logger.info(sys_userList.size()+" " + uname + " " + pwd);
        if(sys_userList.size()==1){
            apiData.code = 1;
            apiData.msg ="T!";
            sys_user user = sys_userList.get(0);
            String token = ApiService.getToken(user.getId()+"");
            boolean b = ApiService.isLoginByApi(user.getId()+"",token);
            Logger.info(b+"");
            apiData.data.put("token",token);
            apiData.data.put("user",user);
        }else{
            apiData.code = -1; // 0 安卓端会跳转登录, 登录这里错误给-1;
            apiData.msg="用户名或密码错误";
        }
        renderJSON(apiData);
    }
    public static void listCity(int pageSize){
        if(pageSize<0){
            pageSize = 0;
        }
        List<sys_city> cityList = sys_city.find(" from sys_city").fetch(pageSize,10);
        ApiData apiData = new ApiData();
        apiData.success();
        apiData.data.put("list",cityList);
        renderJSON(apiData);
    }
    // 退出
    public static void logout(String uid,String token){
        boolean res = ApiService.deleteToken(uid,token);
        ApiData apiData = new ApiData();
        if(res){
            apiData.code=1;
            apiData.msg = "T";
        }
        renderJSON(apiData);
    }
    // 强制自动升级
    public static void isUpdata(String verCode){
        String localVercode = "1.0.1"; //1.0.1
        ApiData apiData = new ApiData();
        apiData.code = 1;
        if(!localVercode.equals(verCode)) {
            apiData.msg = "获取升级数据成功";
            apiData.data.put("needUpdata", 0); // 0 需要升级，1不需要升级
            apiData.data.put("downloadUrl", "http://192.168.0.99:9000/public/ysh.apk");
        }else{
            apiData.msg = "获取升级数据成功";
            apiData.data.put("needUpdata", 1); // 0 需要升级，1不需要升级
            apiData.data.put("downloadUrl", "http://192.168.0.99:9000/public/ysh.apk");
        }
        renderJSON(apiData);
    }
}
